﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using AiFrame.InterfaceLib.Data.Patterns;
using AiFrame.InterfaceLib.MVP;
using AiFrame.InterfaceLib.Windows.Controls;

namespace AiFrame.Windows.Controls.NavigationBar
{
    public enum NavBarButtonState
    {
        Normal,
        Hovered,
        Pressed
    } ;

    public partial class NavigationBarButton : UserControl, IObservable
    {
        private readonly string _hoverText;
        private readonly Image _image;
        private string caption;
        private NavBarButtonState navBarButtonState;
        private List<IObserver> observerCollection;
        private readonly INavigationBarTheme _theme;
        private IDataNavigationBar _navigationBar;
        private OnClickProcedure _onClickProcedure;
        
        public NavigationBarButton(INavigationBarTheme theme, string name, string caption, string hoverText,
                                   Image image, IDataNavigationBar navigationBar)
        {
            /* Initialisierungen */
            this._hoverText = hoverText;
            this._image = image;
            this.Name = name;
            this._theme = theme;
            this._navigationBar = navigationBar;
            InitializeComponent();
            observerCollection = new List<IObserver>();
            /* OnClick registrieren */
            backPictureBox.MouseClick +=new MouseEventHandler(OnButtonClick);

            Caption = caption;
            
            Init();
            UpdateControls();
        }

        public OnClickProcedure OnClickProcedure { get; set; }

        private void OnButtonClick(object sender, MouseEventArgs e)
        {
            _navigationBar.BringWindowToFront();
            if (OnClickProcedure != null)
                OnClickProcedure(this, new EventArgs());
        }

        private void UpdateControls()
        {
            statusLabel.Text = caption;
            
        }

        private void Init()
        {
            State = NavBarButtonState.Normal;

        }

        public IDataNavigationBar DataNavigationbar
        {
            get
            {
                return _navigationBar;
            }
        }

        private void backPictureBox_Paint(object sender, PaintEventArgs e)
        {
            /* Dieser Teil wird dazu benutzt, dass der Hintergrund
             * der Labels auf der PictureBox transparent erscheint. */
            foreach (Control C in this.Controls)
            {
                if (C is Label)
                {
                    Label L = (Label) C;
                    L.Visible = false;
                    e.Graphics.DrawString(L.Text, _theme.GetNavBarButtonFont(), new
                                                              SolidBrush(_theme.GetNavBarButtonFontColor()), 
                                                              L.Left - backPictureBox.Left,
                                          L.Top - backPictureBox.Top);
                }
            }

            /* Icon zeichnen */
            if (_image != null)
            {
                e.Graphics.DrawImage(_image, 10, 8, _image.Width, _image.Height);
            }
                

        }
        
        public NavBarButtonState State
        {
            get
            {
                return navBarButtonState;
            }
            set
            {
                navBarButtonState = value;
                Image curImage = null;

                switch (value)
                {
                    case NavBarButtonState.Normal:
                        curImage = _theme.GetNavBarButtonImageNormal();
                        break;
                    case NavBarButtonState.Hovered:
                        curImage = _theme.GetNavBarButtonImageHovered();
                        break;
                    case NavBarButtonState.Pressed:
                        curImage = _theme.GetNavBarButtonImagePressed();
                        NotifyObservers(null);
                        break;
                }
                backPictureBox.Image = curImage;
            }
        }

        private void backPictureBox_MouseEnter(object sender, EventArgs e)
        {
            if (State != NavBarButtonState.Pressed)
                State = NavBarButtonState.Hovered;
        }

        private void backPictureBox_MouseLeave(object sender, EventArgs e)
        {
            if (State != NavBarButtonState.Pressed)
                State = NavBarButtonState.Normal;
        }

        public string Caption
        {
            set
            {
                caption = value;
            }
            get
            {
                return caption;
            }
        }

        #region Implementation of IObservable

        public void NotifyObservers(object obj)
        {
            foreach(IObserver obs in observerCollection)
            {
                obs.NotifyChanges(this, obj);
            }
        }

        public void RegisterObserver(IObserver obs)
        {
            if (observerCollection.IndexOf(obs) == -1)
            {
                observerCollection.Add(obs);
            }
        }

        public void UnregisterObserver(IObserver obs)
        {
            observerCollection.Remove(obs);
        }

        public void BeginUpdate()
        {
            throw new System.NotImplementedException();
        }

        public void EndUpdate()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void backPictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (State == NavBarButtonState.Hovered)
                State = NavBarButtonState.Pressed;
        }
    }
}